package consys.social.core;

import consys.social.core.bo.Entity;
import consys.social.core.bo.Wall;
import consys.social.core.bo.WallPost;
import consys.social.core.bo.WallPostType;
import consys.social.core.dao.FollowDaoImpl;
import consys.social.core.dao.WallDaoImpl;
import consys.social.core.service.AuthenticatedUser;
import consys.social.core.service.FollowServiceImpl;
import consys.social.core.service.WallServiceImpl;
import consys.social.memcached.dao.spymemcached.SpyMemcachedImpl;
import consys.social.nosql.dao.hbase.HBase;
import consys.social.nosql.dao.hbase.HBaseTable;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Walle extends HttpServlet{
    private static final long serialVersionUID = -3049636220738739954L;

    @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException{

        String [] servers = {"localhost:11211"};
        SpyMemcachedImpl mem = new SpyMemcachedImpl("binary", servers);
        mem.setCacheOperationTimeoutSeconds(2);
        mem.setDefaultExpirationTimeSeconds(3600);

        ThreadLocal<HBase> ent = new ThreadLocal<HBase>();
        ThreadLocal<HBase> wal = new ThreadLocal<HBase>();
        ThreadLocal<HBase> disc = new ThreadLocal<HBase>();
        HBaseTable he = new HBaseTable("entities");
        HBaseTable hw = new HBaseTable("walls");
        HBaseTable hd = new HBaseTable("discussions");
        ent.set(he);
        wal.set(hw);
        disc.set(hd);

        FollowServiceImpl fs = new FollowServiceImpl();
        FollowDaoImpl fd = new FollowDaoImpl();
        fd.setEntities(ent);
        fd.setMemcached(mem);
        fs.setFollowDao(fd);

        WallServiceImpl ws = new WallServiceImpl();
        WallDaoImpl wd = new WallDaoImpl();
        wd.setDiscussions(disc);
        wd.setMemcached(mem);
        wd.setWalls(wal);
        wd.setStoreSeconds(3600);
        ws.setFollowService(fs);
        ws.setWallDao(wd);


        PrintWriter out = response.getWriter();

        List<Entity> entities = new ArrayList<Entity>();
        for (int i = 0; i < 5; i++) {
            entities.add(new Entity(new Integer(i).toString()));
        }
        Entity f = new Entity("f");
        AuthenticatedUser.setUser(f);
        for (Entity a : entities) {
            try {
                fs.follow(f, a);
            } catch (NotAuthorizedException ex) {
            }
        }

        Wall wall = new Wall ();

        WallPost wp = new WallPost();
        for (int i = 0; i < 10; i++) {
            for (Entity a : entities) {
                AuthenticatedUser.setUser(a);
                wp.setAddedBy(a);
                wp.setDate(new Date());
                wp.setText(new Integer(i).toString());
                wp.setType(new WallPostType(1));
                try {
                    ws.postPost(wp);
                } catch (NotAuthorizedException ex) {
                }
            }
        }
        AuthenticatedUser.setUser(f);
        try {
            wall = ws.loadEntityWallNewest(f, 30);
        } catch (NotAuthorizedException ex) {
        }
        out.println("<html>");
        out.println("<body>");
        for (int i = 0; i < 30; i++) {
            out.println(wall.getWallPosts().get(i).getText());
            out.println("<br>");
        }
        out.println("</body>");
        out.println("</html>");
   }
}
